package q36_isValidSudoku;

import java.util.HashSet;

public class solution {
    public static void main(String[] args) {
        HashSet<Integer> set = new HashSet<>();
        set.add(1);
        set.add(2);
        boolean a = set.add(1);
        System.out.println(a);
        System.out.println(set);
    }
    public static boolean isValidSudoku(char[][] board) {
// check row
        for (int i = 0; i<9; i++) {
            int[] check = {0,0,0,0,0,0,0,0,0};
            for (int j = 0; j<9; j++) {
                if (board[i][j] == '.') continue;
                if (check[board[i][j] - '1'] != 0) {
                    return false;
                }
                // 这一行的意思就是把对应数字的位置改成1，也就是该数字已经出现过了
                // 所以当判断check[board[i][j] - '1'] != 0如果为真，就意味着有数字在同一行出现了两次
                check[board[i][j]-'1'] = 1;
            }
        }
        // check col
        for (int i = 0; i<9; i++) {
            int[] check = {0,0,0,0,0,0,0,0,0};
            for (int j = 0; j<9; j++) {
                if (board[j][i] == '.') continue;
                if (check[board[j][i] - '1'] != 0) return false;
                check[board[j][i]-'1'] = 1;
            }
        }
        // check block

        for (int i = 0; i<3; i++) {
            for (int j = 0; j<3; j++) {
                int[] check = {0,0,0,0,0,0,0,0,0};
                for (int ii = i*3; ii<i*3+3; ii++) {
                    for (int jj=j*3; jj<j*3+3; jj++) {
                        if (board[ii][jj] == '.') continue;
                        if (check[board[ii][jj] - '1'] != 0) return false;
                        check[board[ii][jj]-'1'] = 1;
                    }
                }
            }
        }

        return true;
    }
}
